// formPackage/pages/newHousePublishModule/housePublishFormatList/housePublishFormatList.js

import { queryNewHouseFloorList, deleteNewHouseFloor } from "../../../../service/publish_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseNumber: "",
    floorList: [
     
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber:options.houseNumber
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryNewHouseFloorList();
  },
  //查询已有楼栋列表
  queryNewHouseFloorList() {
    wx.showLoading();
    queryNewHouseFloorList(this.data.houseNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:已有楼栋:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          floorList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.log("JJLog:楼栋列表失败:" + JSON.stringify(err));
    })
  },






  //编辑楼栋数据:
  editFloorAction(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/formPackage/pages/newHousePublishModule/addNewFloor/addNewFloor?id=' + item.id + "&houseNumber=" + this.data.houseNumber,
    })
  },

  //删除楼栋数据:
  deleteFloorAction(e) {
    let item = e.currentTarget.dataset.item;
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除该楼栋',
      success(res) {
        if (res.confirm) {
          that.deleteNewHouseFloor(item.id);
        }
      }
    })
  },
  //删除楼栋请求:
  deleteNewHouseFloor(id) {
    wx.showLoading();
    deleteNewHouseFloor(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        this.queryNewHouseFloorList();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '操作失败',
      })
    })
  },



  // 新增楼栋跳转:
  addNewFloor() {
    wx.navigateTo({
      url: '/formPackage/pages/newHousePublishModule/addNewFloor/addNewFloor?houseNumber=' + this.data.houseNumber,
    })
  },
  //去创建楼栋
  createFloor() {
    wx.navigateBack({
      delta: 4,
    })
  },

})